package com.ruoyi.web.controller.frontierEvent;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.basicInformation.service.ITAlarmService;
import com.ruoyi.system.basicInformation.vo.TAlarmVo;
import com.ruoyi.system.frontierEvent.bo.TEventAddBo;
import com.ruoyi.system.frontierEvent.bo.TEventEditBo;
import com.ruoyi.system.frontierEvent.bo.TEventQueryBo;
import com.ruoyi.system.frontierEvent.service.ITEventService;
import com.ruoyi.system.frontierEvent.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 案件信息Controller
 * 
 * @author ruoyi
 * @date 2021-05-19
 */
@Api(value = "案件信息控制器", tags = {"案件信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/frontierEvent/event")
public class TEventController extends BaseController {

    private final ITEventService iTEventService;



    /**
     * 查询案件信息列表
     */
    @ApiOperation("查询案件信息列表")
    @GetMapping("/list")
    public TableDataInfo<TEventVo> list(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 事件上报审核查询案件信息列表
     */
    @ApiOperation("事件上报审核查询案件信息列表")
    @GetMapping("/queryAuditList")
    public TableDataInfo<TEventVo> queryAuditList(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.queryAuditList(bo);
        return getDataTable(list);
    }

    /**
     * 事件上报审核查询案件信息列表
     */
    @ApiOperation("事件上报审核查询案件信息列表")
    @GetMapping("/queryAuditListForMap")
    public TableDataInfo<TEventVo> queryAuditListForMap(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.queryAuditListForMap(bo);
        return getDataTable(list);
    }

    @ApiOperation("案件信息来源统计")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:infoSourceStatistics')")
    @GetMapping("/listEventInfoSourceStatistics")
    public TableDataInfo<TEventVo> listEventInfoSourceStatistics(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.listEventInfoSourceStatistics(bo);
        return getDataTable(list);
    }

    @ApiOperation("违法、违规信息分类统计")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:violationStatistics')")
    @GetMapping("/listViolationStatistics")
    public TableDataInfo<TEventVo> listViolationStatistics(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.listViolationStatistics(bo);
        return getDataTable(list);
    }

    @ApiOperation("分类统计")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:typeStatistics')")
    @GetMapping("/typeStatistics")
    public TableDataInfo<TEventVo> typeStatistics(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.typeStatistics(bo);
        return getDataTable(list);
    }

    @ApiOperation("统计分析")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:analysisStatistics')")
    @GetMapping("/analysisStatistics")
    public TableDataInfo<TEventVo> analysisStatistics(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.analysisStatistics(bo);
        return getDataTable(list);
    }

    @ApiOperation("报警情况分析")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:policeInformationStatistics')")
    @GetMapping("/policeInformationStatistics")
    public TableDataInfo<TEventVo> policeInformationStatistics(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.policeInformationStatistics(bo);
        return getDataTable(list);
    }

    @ApiOperation("严重等级分析")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:policeInformationStatistics')")
    @GetMapping("/policeLevelStatistics")
    public TableDataInfo<TEventVo> policeLevelStatistics(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.policeLevelStatistics(bo);
        return getDataTable(list);
    }

    @ApiOperation("导出案件信息来源统计")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:infoSourceStatisticsExport')")
    @Log(title = "案件信息来源统计", businessType = BusinessType.EXPORT)
    @GetMapping("/infoSourceStatisticsExport")
    public AjaxResult<TEventVo> infoSourceStatisticsExport(TEventQueryBo bo) {
        List<TEventVo> list = iTEventService.listEventInfoSourceStatistics(bo);

        List<TInfoSourceStatisticsExportEventVo> tInfoSourceStatisticsExportEventVoList = new ArrayList<>();
        list.forEach(listVo -> {
            TInfoSourceStatisticsExportEventVo tInfoSourceStatisticsExportEventVo = new TInfoSourceStatisticsExportEventVo();
                BeanUtil.copyProperties(listVo, tInfoSourceStatisticsExportEventVo);

            tInfoSourceStatisticsExportEventVoList.add(tInfoSourceStatisticsExportEventVo);
        });

        ExcelUtil<TInfoSourceStatisticsExportEventVo> util = new ExcelUtil<TInfoSourceStatisticsExportEventVo>(TInfoSourceStatisticsExportEventVo.class);
        return util.exportExcel(tInfoSourceStatisticsExportEventVoList, "案件信息来源统计");
    }

    @ApiOperation("导出违法、违规信息分类统计")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:violationStatisticsExport')")
    @Log(title = "违法、违规信息分类统计", businessType = BusinessType.EXPORT)
    @GetMapping("/violationStatisticsExport")
    public AjaxResult<TEventVo> violationStatisticsExport(TEventQueryBo bo) {
        List<TEventVo> list = iTEventService.listViolationStatistics(bo);
        List<TViolationStatisticsExportEventVo> tViolationStatisticsExportEventVoList= new ArrayList<>();
        list.forEach(listVo -> {
            TViolationStatisticsExportEventVo tViolationStatisticsExportEventVo = new TViolationStatisticsExportEventVo();
            BeanUtil.copyProperties(listVo, tViolationStatisticsExportEventVo);

            tViolationStatisticsExportEventVoList.add(tViolationStatisticsExportEventVo);
        });

        ExcelUtil<TViolationStatisticsExportEventVo> util = new ExcelUtil<TViolationStatisticsExportEventVo>(TViolationStatisticsExportEventVo.class);
        return util.exportExcel(tViolationStatisticsExportEventVoList, "违法、违规信息分类统计");
    }

    @ApiOperation("导出分类统计")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:typeStatisticsExport')")
    @Log(title = "分类统计", businessType = BusinessType.EXPORT)
    @GetMapping("/typeStatisticsExport")
    public AjaxResult<TEventVo> typeStatisticsExport(TEventQueryBo bo) {
        List<TEventVo> list = iTEventService.typeStatistics(bo);
        List<TTypeStatisticsExportEventVo> tTypeStatisticsExportEventVoList= new ArrayList<>();
        list.forEach(listVo -> {
            TTypeStatisticsExportEventVo tTypeStatisticsExportEventVo = new TTypeStatisticsExportEventVo();
            BeanUtil.copyProperties(listVo, tTypeStatisticsExportEventVo);
            tTypeStatisticsExportEventVo.setEvtTypeRate(tTypeStatisticsExportEventVo.getEvtTypeRate()+"%");
            tTypeStatisticsExportEventVoList.add(tTypeStatisticsExportEventVo);
        });

        ExcelUtil<TTypeStatisticsExportEventVo> util = new ExcelUtil<TTypeStatisticsExportEventVo>(TTypeStatisticsExportEventVo.class);
        return util.exportExcel(tTypeStatisticsExportEventVoList, "分类统计");
    }

    /**
     * 导出案件信息列表
     */
    @ApiOperation("导出案件信息列表")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:export')")
    @Log(title = "案件信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<TEventVo> export(TEventQueryBo bo) {
        startPage();
        List<TEventVo> list = iTEventService.querypListForExport(bo);
        ExcelUtil<TEventVo> util = new ExcelUtil<TEventVo>(TEventVo.class);
        return util.exportExcel(list, "案件信息");
    }

    /**
     * 导出案件信息列表
     */
    @ApiOperation("导出案件信息列表")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:export')")
    @Log(title = "案件信息", businessType = BusinessType.EXPORT)
    @GetMapping("/exportsf")
    public AjaxResult<TEventSFVo> exportsf(TEventQueryBo bo) {
        List<TEventVo> list = iTEventService.querypListForExport(bo);

        List<TEventSFVo> listSF= new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {

            TEventSFVo add = BeanUtil.toBean(list.get(i), TEventSFVo.class);
            listSF.add(add);
        }

        ExcelUtil<TEventSFVo> util = new ExcelUtil<TEventSFVo>(TEventSFVo.class);
        return util.exportExcel(listSF, "巡护上报");
    }

    /**
     * 获取案件信息详细信息
     */
    @ApiOperation("获取案件信息详细信息")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:query')")
    @GetMapping("/{id}")
    public AjaxResult<TEventVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTEventService.queryById(id));
    }

    /**
     * 新增案件信息
     */
    @ApiOperation("新增案件信息")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:add')")
    @Log(title = "案件信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult<Void> add(@RequestBody TEventAddBo bo) {
        return toAjax(iTEventService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 新增案件信息
     */
    @ApiOperation("报警生成案件信息")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:add')")
    @Log(title = "报警生成案件信息", businessType = BusinessType.INSERT)
    @PostMapping("/alarmAddEvtInfo")
    public AjaxResult<Void> alarmAddEvtInfo(@RequestBody TEventAddBo bo) {

        return toAjax(iTEventService.alarmAddEvtInfo(bo) ? 1 : 0);
    }

    /**
     * 修改案件信息
     */
    @ApiOperation("修改案件信息")
//    @PreAuthorize("@ss.hasPermi('frontierEvent:event:edit')")
    @Log(title = "案件信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult<Void> edit(@RequestBody TEventEditBo bo) {
        return toAjax(iTEventService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除案件信息
     */
    @ApiOperation("删除案件信息")
    @PreAuthorize("@ss.hasPermi('frontierEvent:event:remove')")
    @Log(title = "案件信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        return toAjax(iTEventService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
